package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;



@Repository("serviceMajorDAO")
public class ServiceMajorDAO extends BaseDaoImpl<ServiceMajor>{
	/**
	 * 根据条件查找分页
	 * @param serviceMajor 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<ServiceMajor> find(ServiceMajor serviceMajor, int page, int size){
		String queryString = "from ServiceMajor where 1=1";
		if(null != serviceMajor.getMajorId() && !"".equals(serviceMajor.getMajorId())){			
			queryString = queryString + "and majorId like '%"+serviceMajor.getMajorId()+"%'";
		}
		if(null != serviceMajor.getMajorName() && !"".equals(serviceMajor.getMajorName())){
			queryString = queryString +" and majorName like '%"+serviceMajor.getMajorName()+"%'";
		}
		if(null != serviceMajor.getAcademyId() && !"".equals(serviceMajor.getAcademyId())){
			queryString = queryString +" and academyId like '%"+serviceMajor.getAcademyId()+"%'";
		}
		int start=(page-1)*size;
		int limit =size;
		return (Result<ServiceMajor> )super.find(queryString, null, null, start, limit);
	}
	
	public List<ServiceMajor> findAll() {
		List<ServiceMajor> serviceClassList = null;
		String queryString = "from ServiceMajor where 1=1";
		serviceClassList = this.getHibernateTemplate().find(queryString);
		return serviceClassList;
	}
	/**
	 * 根据id查询单个用户
	 * @param userId
	 * @return
	 */
	public ServiceMajor findById(int id){
		List<ServiceMajor> serviceClassList = null;
		String queryString = "from ServiceMajor where id ="+id;
		try{
			serviceClassList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return serviceClassList.get(0);
	}
	
	public List<ServiceMajor> findByProperty(String property, String value){
		List<ServiceMajor> list = null;
		String queryString = "from ServiceMajor where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}

	/**
	 * 查找专业列表
	 * @param academyId
	 * @return
	 */
	public List<ServiceMajor> findAllMajorByAcademy(String academyId) {
		// TODO Auto-generated method stub
		String queryString = "from ServiceMajor where academyId = ?";		
		return findByHql(queryString, new Object[]{academyId});
	}
}